home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / kiss-0.11 / kiss-0 / kiss / src / dokiss.c < prev    next >
C/C++ Source or Header  |  1995-04-12  |  3KB  |  129 lines

  1. #include "kiss.h"
  2.  
  3. int dokiss (int argc, char **argv)
  4. {
  5.     register int
  6.     cflag = 0,
  7.     opt;
  8.     struct passwd
  9.     *pwd;
  10.     char
  11.     buf [LINELEN];
  12.  
  13.     orgargc = argc;
  14.     orgargv = argv;
  15.     
  16.     while ( (opt = getopt (argc, argv, "cCdeEhkv")) != -1 && ! cflag)
  17.     switch (opt)
  18.     {
  19.         case 'c':
  20.         cflag = 1;
  21.         break;
  22.         case 'd':
  23.         flags.debug = 1;
  24.         break;
  25.         case 'C':
  26.         flags.ctrlc = 1;
  27.         break;
  28.         case 'v':
  29.         flags.version = 1;
  30.         break;
  31.         case 'e':
  32.         flags.supressstat = 1;
  33.         break;
  34.         case 'E':
  35.         flags.noenviron = 1;
  36.         break;
  37.         case 'k':
  38.         flags.controlkids = 1;
  39.         break;
  40.         case 'h':
  41.         default:
  42.         error ("Bad commandline.\n"
  43.                "Usage: %s [-cCdeEhkv] [file(s)]\n"
  44.                "Where:\n"
  45.                "    -c cmd: run command and terminate, must be "
  46.                                 "last argument\n"
  47.                "    -C: exit on ^C, default is to ignore\n"
  48.                "    -d: debug run, no program started\n"
  49.                "    -e: suppress printing exit status of programs\n"
  50.                "    -E: suppress setting of environment variables\n"
  51.                "    -h: this message\n"
  52.                "    -k: keep control of background processes, "
  53.                             "kill when finished\n"
  54.                "        (default: release background processes)\n"
  55.                "    -v: print version and stop\n"
  56.                "     file(s): scripts to interpret, when absent: "
  57.                                 "stdin is used\n"
  58.                , progname);
  59.         }
  60.     
  61.     if (flags.version)
  62.     banner ();
  63.     
  64.     if (flags.ctrlc)
  65.     signal (SIGINT, sighandler);
  66.     else
  67.     signal (SIGINT, SIG_IGN);
  68.  
  69.     signal (SIGUSR1, sighandler);
  70.     signal (SIGUSR2, sighandler);
  71.     signal (SIGSEGV, sighandler);
  72.  
  73.     if (! flags.noenviron)            /* set environ strings */
  74.     {
  75.     addtoenv ("SHELL", argv [0]);        /* SHELL */
  76.     setshlvl ();                /* SHLVL */
  77.  
  78.     if (! (pwd = getpwuid (getuid ())) )
  79.         warning ("failure while reading user info");
  80.     else                    /* USER and UID and HOME */
  81.     {
  82.         addtoenv ("USER", pwd->pw_name);
  83.         strcpy (username, pwd->pw_name);
  84.         sprintf (buf, "%d", pwd->pw_uid);
  85.         addtoenv ("UID", buf);
  86.         if (pwd->pw_dir)
  87.         {
  88.         addtoenv ("HOME", pwd->pw_dir);
  89.         strcpy (homedir, pwd->pw_dir);
  90.         }
  91.     }
  92.  
  93.     if (ttyname (STDIN_FILENO))        /* TTY */
  94.         addtoenv ("TTY", ttyname (STDIN_FILENO));
  95.     }
  96.  
  97.     /* -c flag given? */
  98.     if (cflag)
  99.     {
  100.     if (optind >= argc)
  101.         error ("-c flag needs command to run");
  102.     command (optind);
  103.     }
  104.     else
  105.     {
  106.     /* more file args? */
  107.     if (optind < argc)
  108.     {
  109.         if (! (yyin = fopen (argv [optind], "r")) )
  110.         warning ("cannot open script \"%s\" for reading",
  111.              argv [optind]);
  112.         else
  113.         {
  114.         lastfile = optind;
  115.         yyparse ();
  116.         }
  117.     }
  118.     else
  119.     {
  120.         yypushfile (stdin);
  121.         startupfiles ();
  122.         yyparse ();
  123.         if (isatty (fileno (yyin)))
  124.         putchar ('\n');
  125.     }
  126.     }    
  127.     return (laststatus);
  128. }
  129.